package com.yalu.action.data;


import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.yalu.action.base.BaseAction;
import com.yalu.entity.base.BaseEntity;
import com.yalu.service.data.BaseDataService;
import com.yalu.util.BaseExcelImportUtil;
import com.yalu.util.FileUtil;

@ParentPackage("data")
public class ExcelImportAction extends BaseAction{
	
	private static final long serialVersionUID = -6389487077410318697L;
	private File excelFile; // 页面传过来要解析的xml文件
	private String excelFileFileName; // 文件名
	private String dataType;//数据类型
	private String importResult;//导入结果
	private int saveCount;//有效导入数量
	private int updateCount;//覆盖更新数量
	private List<String> errMsg;
	@Resource
	private BaseDataService baseDataService;
	
	/**
	* @Description: 导入主页
	* @return
	* @date 2013-10-28 下午07:19:24
	* @author zch
	*/
	public String index(){
		pageFlag = "index";
		return "index";
	}
	
	public String list(){
		pageFlag = "list";
		return "list";
	}
	
	public String importExcel(){
		if(excelFile!=null){
			String savePath = getApplication().getRealPath("/importExcel");
			File file = FileUtil.saveFile(savePath, excelFile, excelFileFileName);
			BaseExcelImportUtil util = BaseExcelImportUtil.createUtilByDataType(dataType);
			errMsg = util.importExcel(excelFile.getAbsolutePath());
			System.out.println(util.getClass().getSimpleName());
			if(errMsg.size()==0){
				List<BaseEntity> entityList = util.getDataList();
				int[] returnCounts = baseDataService.save(dataType,entityList);
				saveCount = returnCounts[0];
				updateCount = returnCounts[1];
				pageFlag = "success";
			}else{
				FileUtil.deleteFile(file);
				pageFlag = "error";
				System.out.println("pageFlag："+pageFlag);
			}
		}
		return "index";
	}


	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getImportResult() {
		return importResult;
	}

	public void setImportResult(String importResult) {
		this.importResult = importResult;
	}

	public File getExcelFile() {
		return excelFile;
	}

	public void setExcelFile(File excelFile) {
		this.excelFile = excelFile;
	}

	public String getExcelFileFileName() {
		return excelFileFileName;
	}

	public void setExcelFileFileName(String excelFileFileName) {
		this.excelFileFileName = excelFileFileName;
	}

	public int getSaveCount() {
		return saveCount;
	}

	public void setSaveCount(int saveCount) {
		this.saveCount = saveCount;
	}

	public List<String> getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(List<String> errMsg) {
		this.errMsg = errMsg;
	}

	public int getUpdateCount() {
		return updateCount;
	}

	public void setUpdateCount(int updateCount) {
		this.updateCount = updateCount;
	}

}
